using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Dare.Utilities
{
    public class AssemblyUtil
    {
        public static Version GetAssemblyVersion()
        {
            return Assembly.GetCallingAssembly().GetName().Version;
        }

        public static T GetAssemblyAttribute<T>()
            where T : System.Attribute
        {
            object[] attributes = Assembly.GetCallingAssembly().GetCustomAttributes(typeof(T), false);
            if (attributes.Length > 0)
            {
                return attributes[0] as T;
            }
            else
            {
                return null;
            }
        }

        public static T GetAssemblyAttribute<T>(Assembly assembly)
            where T : System.Attribute
        {
            object[] attributes = assembly.GetCustomAttributes(typeof(T), false);
            if (attributes.Length > 0)
            {
                return attributes[0] as T;
            }
            else
            {
                return null;
            }
        }

        public static string GetAssemblyTitle()
        {
            AssemblyTitleAttribute attrib = GetAssemblyAttribute<AssemblyTitleAttribute>(Assembly.GetCallingAssembly());
            return attrib == null ? String.Empty : attrib.Title;
        }

        public static string GetAssemblyDescription()
        {
            AssemblyDescriptionAttribute attrib = GetAssemblyAttribute<AssemblyDescriptionAttribute>(Assembly.GetCallingAssembly());
            return attrib == null ? String.Empty : attrib.Description;
        }

        public static string GetAssemblyProduct()
        {
            AssemblyProductAttribute attrib = GetAssemblyAttribute<AssemblyProductAttribute>(Assembly.GetCallingAssembly());
            return attrib == null ? String.Empty : attrib.Product;
        }

        public static string GetAssemblyCopyright()
        {
            AssemblyCopyrightAttribute attrib = GetAssemblyAttribute<AssemblyCopyrightAttribute>(Assembly.GetCallingAssembly());
            return attrib == null ? String.Empty : attrib.Copyright;
        }

        public static string GetAssemblyCompany()
        {
            AssemblyCompanyAttribute attrib = GetAssemblyAttribute<AssemblyCompanyAttribute>(Assembly.GetCallingAssembly());
            return attrib == null ? String.Empty : attrib.Company;
        }
    }
}
